<template>
	<div v-cloak class="aui-wrapper aui-page__iframe">
		<div class="aui-content__wrapper">
			<div class="aui-content">
				<el-card shadow="never" class="aui-card--fill">
					<div class="mod-job__schedule">
						<el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
							<el-form-item>
								<el-input  size="small" v-model="dataForm.beanName" placeholder="bean名称" clearable></el-input>
							</el-form-item>
							<el-form-item>
								<el-button size="small" @click="getDataList()">查询</el-button>
							</el-form-item>
							<!-- <el-form-item>
	                <el-button v-if="$hasPermission('sys:schedule:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
	              </el-form-item>
	              <el-form-item>
	                <el-button v-if="$hasPermission('sys:schedule:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
	              </el-form-item>
	              <el-form-item>
	                <el-button v-if="$hasPermission('sys:schedule:pause')" type="danger" @click="pauseHandle()">{{ $t('schedule.pauseBatch') }}</el-button>
	              </el-form-item>
	              <el-form-item>
	                <el-button v-if="$hasPermission('sys:schedule:resume')" type="danger" @click="resumeHandle()">{{ $t('schedule.resumeBatch') }}</el-button>
	              </el-form-item>
	              <el-form-item>
	                <el-button v-if="$hasPermission('sys:schedule:run')" type="danger" @click="runHandle()">{{ $t('schedule.runBatch') }}</el-button>
	              </el-form-item>
	              <el-form-item>
	                <el-button v-if="$hasPermission('sys:schedule:log')" type="success" @click="logHandle()">{{ $t('schedule.log') }}</el-button>
	              </el-form-item> -->



							<el-form-item>
								<el-button type="primary" @click="addOrUpdateHandle()" size="small"
										   v-if="hasPermission('sys:schedule:save')">新增</el-button>
							</el-form-item>
							<el-form-item>
								<el-button type="danger" @click="deleteHandle()" size="small"
										   v-if="hasPermission('sys:schedule:delete')">删除</el-button>
							</el-form-item>
							<el-form-item>
								<el-button type="danger" @click="pauseHandle()" size="small"
										   v-if="hasPermission('sys:schedule:pause')">暂停</el-button>
							</el-form-item>
							<el-form-item>
								<el-button type="danger" @click="resumeHandle()" size="small"
										   v-if="hasPermission('sys:schedule:resume')">恢复</el-button>
							</el-form-item>
							<el-form-item>
								<el-button type="danger" @click="runHandle()" size="small"
										   v-if="hasPermission('sys:schedule:run')">执行</el-button>
							</el-form-item>
							<el-form-item>
								<el-button type="success" @click="logHandle()" size="small"
										   v-if="hasPermission('sys:schedule:log')">日志列表</el-button>
							</el-form-item>

						</el-form>
						<el-table v-loading="dataListLoading" :data="dataList" border
							@selection-change="dataListSelectionChangeHandle" @sort-change="dataListSortChangeHandle"
							style="width: 100%;">
							<el-table-column type="selection" header-align="center" align="center" width="50">
							</el-table-column>
							<el-table-column prop="beanName" label="bean名称" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="params" label="参数" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="cronExpression" label="cron表达式" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="remark" label="备注" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="status" label="状态" sortable="custom" header-align="center"
								align="center">
								<template #default="scope">
									<el-tag v-if="scope.row.status === 1" size="small">正常</el-tag>
									<el-tag v-else size="small" type="danger">暂停</el-tag>
								</template>
							</el-table-column>
							<el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
								<template #default="scope">
									<!-- <el-button v-if="$hasPermission('sys:schedule:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
	                  <el-button v-if="$hasPermission('sys:schedule:pause')" type="text" size="small" @click="pauseHandle(scope.row.id)">{{ $t('schedule.pause') }}</el-button>
	                  <el-button v-if="$hasPermission('sys:schedule:resume')" type="text" size="small" @click="resumeHandle(scope.row.id)">{{ $t('schedule.resume') }}</el-button>
	                  <el-button v-if="$hasPermission('sys:schedule:run')" type="text" size="small" @click="runHandle(scope.row.id)">{{ $t('schedule.run') }}</el-button>
	                  <el-button v-if="$hasPermission('sys:schedule:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button> -->

									<el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)"
											   v-if="hasPermission('sys:schedule:update')">更新
									</el-button>
									<el-button type="text" size="small" @click="pauseHandle(scope.row.id)"
											   v-if="hasPermission('sys:schedule:pause')">暂停
									</el-button>
									<el-button type="text" size="small" @click="resumeHandle(scope.row.id)"
											   v-if="hasPermission('sys:schedule:resume')">恢复
									</el-button>
									<el-button type="text" size="small" @click="runHandle(scope.row.id)"
											   v-if="hasPermission('sys:schedule:run')">执行</el-button>
									<el-button type="text" size="small" @click="deleteHandleFn(scope.row.id)"
											   v-if="hasPermission('sys:schedule:delete')">删除
									</el-button>

								</template>
							</el-table-column>
						</el-table>
						<el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit"
							:total="total" layout="total, sizes, prev, pager, next, jumper"
							@size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle">
						</el-pagination>
						<!-- 弹窗, 新增 / 修改 -->
						<ScheduleAddOrUpdate v-if="addOrUpdateVisible" ref="addOrUpdate"
							@refresh-data-list="getDataList">
						</ScheduleAddOrUpdate>
						<!-- 弹窗, 日志列表 -->
						<ScheduleLog v-if="logVisible" ref="logRef"></ScheduleLog>
					</div>
				</el-card>
			</div>
		</div>
	</div>
</template>

<script>
	import DataList from "@/utils/DataList";

	import ScheduleAddOrUpdate from "@/components/lswms/job/ScheduleAddOrUpdate.vue"
	import ScheduleLog from "@/views/lswms/job/ScheduleLog.vue"

	import {
		ref,
		reactive,
		onMounted,
		nextTick
	} from 'vue';
	import {
		ElMessage,
		ElMessageBox
	} from 'element-plus'

	import {
		putSchedulePause,
		putScheduleResume,
		putScheduleRun,
		deleteSchedule
	} from "@/network/lswms/job/Schedule.js"
	import {hasPermission} from "@/utils/hasPermission"
	export default {
		name: "schedule",

		components: {
			ScheduleAddOrUpdate,
			ScheduleLog,

		},

		setup() {
			const {
				dataForm,
				mixinViewModuleOptions,
				getDataList,
				dataList,
				total,
				page,
				limit,
				dataListLoading,
				dataListSelectionChangeHandle,
				dataListSortChangeHandle,
				pageSizeChangeHandle,
				pageCurrentChangeHandle,
				addOrUpdateHandle,
				deleteHandle,
				exportHandle,
				addOrUpdateVisible,
				dataListSelections,
				addOrUpdate
			} = DataList();

			onMounted(() => {
				mixinViewModuleOptions.value.getDataListIsPage = true;
				mixinViewModuleOptions.value.getDataListURL = '/sys/schedule/page';
				mixinViewModuleOptions.value.deleteURL = '/sys/schedule';
				// mixinViewModuleOptions.value.exportURL = '/lswms/orders/export';
				mixinViewModuleOptions.value.deleteIsBatchKey = 'id';
				mixinViewModuleOptions.value.deleteIsBatch = true
				if (mixinViewModuleOptions.value.activatedIsNeed) {
					getDataList();
				}
				// ordersStatusFn();
				// orderypeFn();
			})

			// 暂停
			const pauseHandle = (id) => {
				if (!id && dataListSelections.value.length <= 0) {
					return ElMessage({
						message: '请选择Job项',
						type: 'warning',
						duration: 1000
					});
				}

				ElMessageBox.confirm('确定要暂停所选的Job项吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {

					let para = !!id ? [{
							"id": id
						}] : dataListSelections.value.map(function(item) {
							return {"id": item["id"]}
						})


					putSchedulePause(para).then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.data.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								getDataList();
							}
						});
					}).catch(function() {});

				}).catch(function() {});
			}

			// 恢复
			const resumeHandle = (id) => {
				if (!id && dataListSelections.value.length <= 0) {
					return ElMessage({
						message: '请选择Job项',
						type: 'warning',
						duration: 1000
					});
				}

				ElMessageBox.confirm('确定要恢复所选的Job项吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {

					let para = !!id ? [{
							"id": id
						}] : dataListSelections.value.map(function(item) {
							return {"id": item["id"]}
						})
					putScheduleResume(para).then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.data.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								getDataList();
							}
						});
					}).catch(function() {});
				}).catch(function() {});
			}
			//执行
			const runHandle = (id) => {
				if (!id && dataListSelections.value.length <= 0) {
					return ElMessage({
						message: '请选择执行的Job项',
						type: 'warning',
						duration: 1000
					});
				}

				ElMessageBox.confirm('确定要执行所选的Job项吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					let para = !!id ? [{
							"id": id
						}] : dataListSelections.value.map(function(item) {
							return {"id": item["id"]}
						})

					putScheduleRun(para).then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.data.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								getDataList();
							}
						});
					}).catch(function() {});
				}).catch(function() {});
			}
			
			//删除
			const deleteHandleFn = (id) => {
				if (!id && dataListSelections.value.length <= 0) {
					return ElMessage({
						message: '请选择删除的Job项',
						type: 'warning',
						duration: 1000
					});
				}
			
				ElMessageBox.confirm('确定要删除所选的Job项吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					let para = !!id ? [{
							"id": id
						}] : dataListSelections.value.map(function(item) {
							return {"id": item["id"]}
						})
			
					deleteSchedule(para).then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.data.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								getDataList();
							}
						});
					}).catch(function() {});
				}).catch(function() {});
			}
			
			
			
			// 日志列表

			const logVisible = ref(false);
			const logRef = ref(null);
			const logHandle = () => {
				logVisible.value = true;
				nextTick(function() {
					logRef.value.init();
				});
			}

			return {
				dataForm,
				mixinViewModuleOptions,
				getDataList,
				dataList,
				total,
				page,
				limit,
				dataListLoading,
				dataListSelectionChangeHandle,
				dataListSortChangeHandle,
				pageSizeChangeHandle,
				pageCurrentChangeHandle,
				addOrUpdateHandle,
				deleteHandle,
				exportHandle,
				addOrUpdateVisible,
				dataListSelections,
				addOrUpdate,
				pauseHandle,
				resumeHandle,
				runHandle,
				logVisible,
				logRef,
				logHandle,
				deleteHandleFn
			}
		},
		methods: {
			hasPermission,

			reString: function(val) {
				let n = Number(val)
				if (!isNaN(n)) {
					return n
				} else {
					return val
				}
			}
		}
	}
</script>

<style scoped>

</style>
